package cn.iocoder.yudao.module.product.controller.admin.calculation;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.module.product.controller.admin.calculation.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.calculation.CalculationDO;
import cn.iocoder.yudao.module.product.convert.calculation.CalculationConvert;
import cn.iocoder.yudao.module.product.service.calculation.CalculationService;

@Tag(name =  "管理后台 - 产品碳核算")
@RestController
@RequestMapping("/product/calculation")
@Validated
public class CalculationController {

    @Resource
    private CalculationService calculationService;

    @PostMapping("/create")
    @Operation(summary = "创建产品碳核算")
    @PreAuthorize("@ss.hasPermission('product:calculation:create')")
    public CommonResult<Long> createCalculation(@Valid @RequestBody CalculationCreateReqVO createReqVO) {
        return success(calculationService.createCalculation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品碳核算")
    @PreAuthorize("@ss.hasPermission('product:calculation:update')")
    public CommonResult<Boolean> updateCalculation(@Valid @RequestBody CalculationUpdateReqVO updateReqVO) {
        calculationService.updateCalculation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品碳核算")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:calculation:delete')")
    public CommonResult<Boolean> deleteCalculation(@RequestParam("id") Long id) {
        calculationService.deleteCalculation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品碳核算")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:calculation:query')")
    public CommonResult<CalculationRespVO> getCalculation(@RequestParam("id") Long id) {
        CalculationDO calculation = calculationService.getCalculation(id);
        return success(CalculationConvert.INSTANCE.convert(calculation));
    }

    @GetMapping("/list")
    @Operation(summary = "获得产品碳核算列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('product:calculation:query')")
    public CommonResult<List<CalculationRespVO>> getCalculationList(@RequestParam("ids") Collection<Long> ids) {
        List<CalculationDO> list = calculationService.getCalculationList(ids);
        return success(CalculationConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品碳核算分页")
    @PreAuthorize("@ss.hasPermission('product:calculation:query')")
    public CommonResult<PageResult<CalculationRespVO>> getCalculationPage(@Valid CalculationPageReqVO pageVO) {
        PageResult<CalculationDO> pageResult = calculationService.getCalculationPage(pageVO);
        return success(CalculationConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/getDetails")
    @Operation(summary = "获得产品碳核算详细信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:calculation:query')")
    public CommonResult<CalculationDetailsRespVO> getCalculationDetails(@RequestParam("id") Long id) {
        CalculationDetailsRespVO calculationDetailsRespVO = calculationService.getCalculationDetails(id);
        return success(calculationDetailsRespVO);
    }

    @GetMapping("/count")
    @Operation(summary = "计算产品碳核算排放量")
    @PreAuthorize("@ss.hasPermission('product:calculation:query')")
    public CommonResult<Double> countCalculation(@RequestParam("id") Long id) {
        return success(calculationService.countCalculation(id));
    }


}
